package com.getword.socket;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class Demo1
{
    public static void main(String[] args)
        throws Exception
    {
        RandomAccessFile file =
            new RandomAccessFile("/Users/mac/study/java/project/common-code/network/src/main/resources/test.txt", "rw");
        FileChannel channel = file.getChannel();
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        channel.read(byteBuffer); // channel to buffer

        int length2 = byteBuffer.limit();
        byteBuffer.flip();
        int length = byteBuffer.limit();
        byte[] data = new byte[length];
        byteBuffer.get(data, 0, length);  // get buffer
        System.out.println(new String(data));

        channel.close();
    }

    private static class ServerThread implements Runnable
    {
        private Socket socket = null;

        public ServerThread(Socket socket)
        {
            this.socket = socket;
        }

        public void run()
        {
            // 用来向客户端发送数据
            try
            {
                PrintWriter printWriter = new PrintWriter(socket.getOutputStream());
                // 获取客户端数据
                // 带有缓冲区的输入流，
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));

            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }
}
